<?php

class Redirector {

    private $mTargetLink;
    private $mId;
    private $mAutoRedirect = true;
    private $mErrorCode;
    private $mErrorSection;

    public function __construct($pTarget) {
        $this->mTargetLink = $pTarget;
    }

    private function PrintFormRedirect() {
        $tForm = "\n\n<form name=\"formRedirect\" method=\"post\" action=\"" . $this->mTargetLink . "\" >\n" .
                "\t<input type=\"hidden\" name=\"fId\" value=\"" . $this->mId . "\" />\n" .
                "\t<input type=\"hidden\" name=\"PKName\" value=\"\" />\n" .
                "\t<input type=\"hidden\" name=\"PKValue\" value=\"\" />\n" .
                "\t<input type=\"hidden\" name=\"ERROR_SECTION\" value=\"\" />\n" .
                "\t<input type=\"hidden\" name=\"ERROR_CODE\" value=\"\" />\n" .
                "</form>\n\n";
        echo $tForm;
    }

    public function SetId($pValue) {
        $this->mId = $pValue;
    }

    public function SetTarget($pValue) {
        $this->mTargetLink = $pValue;
    }

    public function SetAutoRedirect($pValue) {
        $this->mAutoRedirect = $pValue;
    }

    public function SetErrorCode($pValue) {
        $this->mErrorCode = $pValue;
    }

    public function SetErrorSection($pValue) {
        $this->mErrorSection = $pValue;
    }

    public function Redirecting($pPK = null) {
        $this->PrintFormRedirect();
        if ($pPK) {
            if ($this->mAutoRedirect) {
                if (CORE_DEVELOPING_MODE) {
                    echo "<input type=\"button\" id=\"ContinueButton\" class=\"button\" value=\"Continue\" onclick=\"Redirect('" . $this->mTargetLink . "', '" . $this->mErrorSection . "', '" . $this->mErrorCode . "');\" />\n\n";
                } else {
                    echo "\n\n<script>\n" .
                    "\tvar tPK= new Array();\n";
                    //KONVERSI ARRAY: PHP VAR -> JS VAR
                    foreach ($pPK as $tKey => $tVal) {
                        echo "\ttPK['" . $tKey . "']= \"" . $tVal . "\"; \n";
                    }
                    echo "\tRedirect(\"" . $this->mTargetLink . "\", '" . $this->mErrorSection . "', \"" . $this->mErrorCode . "\");\n" .
                    "</script>\n";
                }
            }
        } else {
            if ($this->mAutoRedirect) {
                if (CORE_DEVELOPING_MODE) {
                    echo "<input type=\"button\" id=\"ContinueButton\" class=\"button\" value=\"Continue\" onclick=\"Redirect('" . $this->mTargetLink . "', '" . $this->mErrorSection . "', '" . $this->mErrorCode . "');\" />\n\n";
                } else {
                    echo "\n\n<script>\n";
                    echo "\tRedirect(\"" . $this->mTargetLink . "\", '" . $this->mErrorSection . "', \"" . $this->mErrorCode . "\");\n" .
                    "</script>\n";
                }
            }
        }
    }

}

?>
